﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Formality.FormalSystem
{
    public abstract class TermOperator : Proposition
    {
        public TermOperator(string Operator, IEnumerable<Proposition> Operands)
        {
            this.Operands = Operands;
            this.OperatorSymbol = Operator;
        }

        public string OperatorSymbol { get; private set; }
        public IEnumerable<Proposition> Operands { get; private set; }

        #region Proposition overrides
        public override Inference? Matches(Proposition that, Substitution Substitution)
        {
            var thatOp = that as TermOperator;

            if (thatOp != null)
            {
                if (OperatorSymbol == thatOp.OperatorSymbol
                    && Operands.SequenceEqual(thatOp.Operands, (o1, o2) => o1.Matches(o2, Substitution).HasValue))
                {
                    return new Inference();
                }
            }

            return null;
        }

        public override IEnumerable<Proposition> ApplyTemplate(Proposition Template, Proposition Replacement)
        {
            foreach (var Rep in base.ApplyTemplate(Template, Replacement))
                yield return Rep;

            var OpArray = Operands.ToArray();

            for (var i = 0; i < OpArray.Length; i++)
            {
                foreach (var ReplacementOp in OpArray[i].ApplyTemplate(Template, Replacement))
                {
                    var RepArray = new Proposition[OpArray.Length];

                    for (var j = 0; j < OpArray.Length; j++)
                    {
                        if (j == i)
                            RepArray[j] = ReplacementOp;
                        else
                            RepArray[j] = OpArray[j];
                    }

                    yield return NewOfThisType(RepArray);
                }
            }
        }

        public override Proposition Replace(Substitution Substitution)
        {
            return NewOfThisType((from Op in Operands
                                  select Op.Replace(Substitution)).ToList());
        }
        #endregion

        #region Sentence overrides
        public override int HashCode
        {
            get
            {
                var Hash = OperatorSymbol.GetHashCode();

                foreach (var O in Operands)
                    Hash ^= O.GetHashCode();

                return Hash;
            }
        }

        public override bool Equals(Sentence Sentence)
        {
            var that = Sentence as TermOperator;

            if (that == null)
                return false;

            return OperatorSymbol.Equals(that.OperatorSymbol)
                && Operands.SequenceEqual(that.Operands);
        }

        public override void Bind(Bindings Bindings, int Depth)
        {
            foreach (var Op in Operands)
                Op.Bind(Bindings, Depth);
        }
        #endregion

        protected abstract TermOperator NewOfThisType(IEnumerable<Proposition> Operands);
    }
}
